package com.example.finalsystem.Service;

import com.example.finalsystem.Dao.GoodsMapper;
import com.example.finalsystem.Entity.Deal;
import com.example.finalsystem.Entity.Goods;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

@Service
public class GoodsService {
    private SqlSession openSession()
    {
        String resource = "mybatis-config.xml";
        InputStream is = GoodsService.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return   sessionFactory.openSession(true);
    }

    public String canOut(String name,Integer number)
    {
        SqlSession sqlSession = openSession();
        Goods goods = sqlSession.getMapper(GoodsMapper.class).selectByPrimaryKey(name);
        if (goods == null)
        {
            return "没有该货物";
        }
        if (goods.getThreshold() > number)
        {
            return "可以出货";
        }
        if(goods.getGoodsnumber() < number)
        {
            return "货物量不足";
        }
        sqlSession.close();
        return "可以出货";
    }



    public String addGoods(String name,Integer number,Integer threshold){
        SqlSession sqlSession = openSession();
        Goods goods = sqlSession.getMapper(GoodsMapper.class).selectByPrimaryKey(name);
        if (goods != null)
        {
            return "有重名物品，添加失败";
        }
        goods = new Goods();
        goods.setGoodsname(name);
        goods.setGoodsnumber(number);
        goods.setThreshold(threshold);
        sqlSession.getMapper(GoodsMapper.class).insert(goods);
        sqlSession.commit();
        sqlSession.close();
        return "添加成功";
    }

    public String fixGoods(String name,Integer number,Integer threshold){
        SqlSession sqlSession = openSession();
        Goods goods = sqlSession.getMapper(GoodsMapper.class).selectByPrimaryKey(name);
        goods.setGoodsnumber(number);
        goods.setThreshold(threshold);
        sqlSession.getMapper(GoodsMapper.class).updateByPrimaryKey(goods);
        sqlSession.commit();
        sqlSession.close();
        return "修改成功";
    }

    public String deleteGoods(String name){
        SqlSession sqlSession = openSession();
        sqlSession.getMapper(GoodsMapper.class).deleteByPrimaryKey(name);
        sqlSession.commit();
        sqlSession.close();
        return "删除成功";
    }

    public Goods getGoods(String name){
        return  openSession().getMapper(GoodsMapper.class).selectByPrimaryKey(name);
    }
//    分页功能
    public List<Goods> getListOfGoods(int currentPage, int pageSize){
        SqlSession sqlSession =openSession();
        List<Goods> goods =sqlSession.getMapper(GoodsMapper.class).getListOfGoods((currentPage-1)*pageSize,pageSize);
        sqlSession.close();
        return goods;
    }

    public Integer countAll()
    {
        SqlSession sqlSession =openSession();
        int i = openSession().getMapper(GoodsMapper.class).countGoods();
        sqlSession.close();
        return i;
    }
}
